package bg.btanev.eightyfour.client.view;

import bg.btanev.eightyfour.client.presenter.StatsListPresenter.StatsListDisplay;
import bg.btanev.eightyfour.client.utils.SelectionUtils;

import com.google.gwt.event.dom.client.HasChangeHandlers;
import com.google.gwt.user.client.ui.Composite;
import com.google.gwt.user.client.ui.DecoratorPanel;
import com.google.gwt.user.client.ui.Grid;
import com.google.gwt.user.client.ui.HasHorizontalAlignment;
import com.google.gwt.user.client.ui.HasWidgets;
import com.google.gwt.user.client.ui.ListBox;
import com.google.gwt.user.client.ui.VerticalPanel;
import com.google.gwt.user.client.ui.Widget;

/**
 *
 * @author B.Tanev
 *
 */
public class StatsListView extends Composite implements StatsListDisplay {
	private final ListBox selectionData;
	private final VerticalPanel statsDataContainer;

	public StatsListView() {

		final VerticalPanel rootContainer = new VerticalPanel();
		initWidget(rootContainer);

		final DecoratorPanel selectionPanel = new DecoratorPanel();
		rootContainer.add(selectionPanel);
		rootContainer.setCellHorizontalAlignment(selectionPanel, HasHorizontalAlignment.ALIGN_CENTER);
		selectionPanel.setWidth("");

		final Grid selectionContainer = new Grid(1, 1);
		selectionPanel.setWidget(selectionContainer);
		selectionContainer.setCellPadding(5);
		selectionContainer.setWidth("500");

		selectionData = new ListBox();
		selectionContainer.setWidget(0, 0, selectionData);
		selectionData.setWidth("100%");

		statsDataContainer = new VerticalPanel();
		statsDataContainer.setSpacing(5);
		rootContainer.add(statsDataContainer);
	}

	public void clearView() {
		selectionData.setSelectedIndex(-1);
		selectionData.clear();
		statsDataContainer.clear();
	}

	public void setSelectionData(String[] statsSelection) {
		clearView();
		if (statsSelection != null && statsSelection.length > 0) {
			for (String stat : statsSelection) {
				selectionData.addItem(stat, stat);
			}
			selectionData.setSelectedIndex(-1);
		}
	}

	public String getSelectedStat() {
		return SelectionUtils.getSelectedText(selectionData);
	}

	public HasChangeHandlers getSelectionData() {
		return selectionData;
	}

	public HasWidgets getStatsDataContainer() {
		return statsDataContainer;
	}

	@Override
	public Widget asWidget() {
		return this;
	}
}
